from BuildTree import *


# 最大bst子树
def dfs(node: TreeNode):
    """
    :return: 最小值 最大值 是否为bst size
    """
    if not node:
        return inf, -inf, True, 0
    info_l = dfs(node.left)
    info_r = dfs(node.right)
    if info_l[2] and info_r[2] and (info_l[1] < node.val < info_r[0]):
        return min(info_l[0], node.val), max(info_r[1], node.val), True, info_l[3] + info_r[3] + 1
    return inf, -inf, False, max(info_l[3], info_r[3])


root = build_tree_from_level_order([2, 1, 3])
result = dfs(root)
print(result)
